package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialChukuDao;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialChuku;
import com.guodu.service.ViewMaterialChukuService;
import com.guodu.vo.ViewMaterialChukuVo;
import com.guodu.vo.ViewVo;

@Service("viewMateriaChukuService")
public class ViewMateriaChukuServiceImpl implements ViewMaterialChukuService{

	@Resource
	private ViewMaterialChukuDao viewMaterialChukuDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public Map<String, ViewMaterialChukuVo> findByVo(ViewVo vvo) {
		// TODO Auto-generated method stub
		List<Units> funitses = unitsDao.findAllFunits();
		List<ViewMaterialChuku> views = viewMaterialChukuDao.findByVo(vvo);
		Map<String, ViewMaterialChukuVo> map = new HashMap<String, ViewMaterialChukuVo>();
		map.put("all", new ViewMaterialChukuVo(new ViewMaterialChuku()));
		for(Units units : funitses){
			map.put(units.getUnitsId().toString()+"all", new ViewMaterialChukuVo(new ViewMaterialChuku()));
		}
		for(ViewMaterialChuku view:views){
			this.sum(map.get("all"), view);
			this.sum(map.get(view.getFunitsId().toString()+"all"), view);
			map.put(view.getUnitsId().toString(), new ViewMaterialChukuVo(view));
		}
		return map;
	}
	
	private ViewMaterialChukuVo sum(ViewMaterialChukuVo view1,ViewMaterialChuku view2){
		view1.setMonthJianxiuMoney((view1.getMonthJianxiuMoney()!=null?view1.getMonthJianxiuMoney():new BigDecimal(0)).add(view2.getMonthJianxiuMoney()!=null?view2.getMonthJianxiuMoney():new BigDecimal(0)));
		view1.setMonthJigaiMoney((view1.getMonthJigaiMoney()!=null?view1.getMonthJigaiMoney():new BigDecimal(0)).add(view2.getMonthJigaiMoney()!=null?view2.getMonthJigaiMoney():new BigDecimal(0)));
		view1.setYearJianxiuMoney((view1.getYearJianxiuMoney()!=null?view1.getYearJianxiuMoney():new BigDecimal(0)).add(view2.getYearJianxiuMoney()!=null?view2.getYearJianxiuMoney():new BigDecimal(0)));
		view1.setYearJigaiMoney((view1.getYearJigaiMoney()!=null?view1.getYearJigaiMoney():new BigDecimal(0)).add(view2.getYearJigaiMoney()!=null?view2.getYearJigaiMoney():new BigDecimal(0)));
		view1.setMonthMoney((view1.getMonthMoney()!=null?view1.getMonthMoney():new BigDecimal(0)).add(view2.getMonthMoney()!=null?view2.getMonthMoney():new BigDecimal(0)));
		view1.setYearMoney((view1.getYearMoney()!=null?view1.getYearMoney():new BigDecimal(0)).add(view2.getYearMoney()!=null?view2.getYearMoney():new BigDecimal(0)));
		view1.setMonthOtherMoney((view1.getMonthOtherMoney()!=null?view1.getMonthOtherMoney():new BigDecimal(0)).add(view2.getMonthOtherMoney()!=null?view2.getMonthOtherMoney():new BigDecimal(0)));
		view1.setYearOtherMoney((view1.getYearOtherMoney()!=null?view1.getYearOtherMoney():new BigDecimal(0)).add(view2.getYearOtherMoney()!=null?view2.getYearOtherMoney():new BigDecimal(0)));
		view1.setMonthWeihuMoney((view1.getMonthWeihuMoney()!=null?view1.getMonthWeihuMoney():new BigDecimal(0)).add(view2.getMonthWeihuMoney()!=null?view2.getMonthWeihuMoney():new BigDecimal(0)));
		view1.setYearWeihuMoney((view1.getYearWeihuMoney()!=null?view1.getYearWeihuMoney():new BigDecimal(0)).add(view2.getYearWeihuMoney()!=null?view2.getYearWeihuMoney():new BigDecimal(0)));
		return view1;
	}

}
